package com.dubbo.dubbo.Annotation;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import io.netty.util.internal.StringUtil;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.util.Random;

//    注解方式实现类
@DubboService(timeout = 5000)
public class HelloAnnotationServiceImp implements IHelloAnnotationService {
    @Override
    public String HelloBoy(String name)
    {
        StringBuffer sb = new StringBuffer();
        sb.append("Hello,Dear Boy ");
        if(name!=""&& !StringUtil.isNullOrEmpty(name))
        {
            sb.append(name);
        }
        return sb.toString();
    }

    @HystrixCommand(commandProperties = {
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000") })
    @Override
    public String HelloHystrix(String name)
    {
        /*
         * Hystrix超时配置的为2s,当实现类睡眠超过2s，服务调用者将执行服务降级函数
         */
        int nextInt = new Random().nextInt(4000)+2000;//随机的int值，该值介于[0,4000)的区间
        System.out.println("sleep " + nextInt + "ms");
        try {
            Thread.sleep(nextInt);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "HELLO  " + name + " from Dubbo Spring Boot";
    }
}
